package com.microframework.boot.visual.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.jfinal.plugin.activerecord.Page;
import com.microframework.base.web.page.PageQuery;
import com.microframework.boot.visual.model.domain.ComponentLibrary;
import com.microframework.boot.visual.service.IComponentLibrary;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ComponentLibraryImpl implements IComponentLibrary {
	@Override
	public Page<ComponentLibrary> pages(PageQuery<ComponentLibrary> pageQuery) {
		try {
			String select="select * ";
			StringBuilder sqlBuilder = new StringBuilder("FROM " + ComponentLibrary.TABLE_NAME + " WHERE 1=1");
			List<Object> params = new ArrayList<>();
			ComponentLibrary query = pageQuery.getQuery();
			if (query != null) {
				if (StringUtils.hasText(query.getName())) {
					sqlBuilder.append(" AND name LIKE ?");
					params.add("%" + query.getName() + "%");
				}
				
				if (StringUtils.hasText(query.getCategory())) {
					sqlBuilder.append(" AND category = ?");
					params.add(query.getCategory());
				}
				
				if (StringUtils.hasText(query.getType())) {
					sqlBuilder.append(" AND type = ?");
					params.add(query.getType());
				}
				
				if (StringUtils.hasText(query.getIsDel())) {
					sqlBuilder.append(" AND is_del = ?");
					params.add(query.getIsDel());
				}else {
					sqlBuilder.append(" AND is_del = ?");
					params.add("0");
				}
			}else {
				sqlBuilder.append(" AND is_del = ?");
				params.add("0");
			}

			// 设置排序
			String orderBy = StringUtils.hasText(pageQuery.getOrderBy()) ? pageQuery.getOrderBy() : "modify_time";
			String orderDirection = StringUtils.hasText(pageQuery.getOrderDirection()) ? pageQuery.getOrderDirection()
					: "desc";
			sqlBuilder.append(" ORDER BY " + orderBy + " " + orderDirection);
			Page<ComponentLibrary> page=ComponentLibrary.dao.paginate(pageQuery.getPageNumber(), pageQuery.getPageSize(), select,sqlBuilder.toString(), params.toArray());
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询列表异常{}", e.getMessage());
			return new Page<ComponentLibrary>(new ArrayList<>(), 1, 10, 0, 0);
		}
	}

	@Override
	public List<ComponentLibrary> getListByCategory(String category, String keywords) {
		try {
			StringBuilder sqlBuilder = new StringBuilder("select * FROM " + ComponentLibrary.TABLE_NAME + " WHERE category=? ");
			List<Object> params = new ArrayList<>();
			params.add(category);
			if (StringUtils.hasText(keywords)) {
				sqlBuilder.append(" AND name LIKE ?");
				params.add("%" + keywords+ "%");
			}
			sqlBuilder.append(" AND is_del = ?");
			params.add("0");

			// 设置排序
			sqlBuilder.append(" ORDER BY modify_time desc");
			//List<ComponentLibrary> list=ComponentLibrary.dao.find(sqlBuilder.toString(), params.toArray());
			return ComponentLibrary.dao.find(sqlBuilder.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询列表异常{}", e.getMessage());
			return new LinkedList<ComponentLibrary>();
		}
	}

}
